﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PointsManager : MonoBehaviour {
	public GameObject pointPrefab;

	public Text aText;
	public Text bText;
	public Text x1Text;
	public Text y1Text;
	public Text x2Text;
	public Text y2Text;
	public Text outputText;

	public float pointsSpeed;

	public InputField aInputField;
	public InputField bInputField;
	public InputField eInputField;
	public float a;
	public float b;
	public float e;
	public float x1;
	public float x2;
	public float y1;
	public float y2;

	private GameObject aPoint;
	private GameObject bPoint;
	private GameObject x1Point;
	private GameObject x2Point;
	private GameObject y1Point;
	private GameObject y2Point;
	// Start is called before the first frame update
	void Start() {
		//point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		//point.GetComponent<Point>().aimPos = Vector3.one;
		//point.GetComponent<Point>().speed = pointsSpeed;
		a = float.Parse(aInputField.text);
		b = float.Parse(bInputField.text);
		e = float.Parse(eInputField.text);

		x1 = a + 0.382f * (b - a);
		x2 = a + 0.618f * (b - a);
		y1 = Mathf.Cos(x1);
		y2 = Mathf.Cos(x2);

		InitializePoints();
		MovePoint();
		UpdateTexts();
	}

	// Update is called once per frame
	void Update() {

	}

	public void OneStep() {
		CalculatePosition();
		MovePoint();
		UpdateTexts();
	}

	void UpdateTexts() {
		aText.text = "a: " + a;
		bText.text = "b: " + b;
		x1Text.text = "x1: " + x1;
		y1Text.text = "y1: " + y1;
		x2Text.text = "x2: " + x2;
		y2Text.text = "y2: " + y2;
	}

	void MovePoint() {
		aPoint.GetComponent<Point>().ChangeAimPos(a, 0);
		bPoint.GetComponent<Point>().ChangeAimPos(b, 0);
		x1Point.GetComponent<Point>().ChangeAimPos(x1, 0);
		x2Point.GetComponent<Point>().ChangeAimPos(x2, 0);
		y1Point.GetComponent<Point>().ChangeAimPos(x1, y1);
		y2Point.GetComponent<Point>().ChangeAimPos(x2, y2);
	}

	void CalculatePosition() {

		if (b - a <= e) {//满足要求
			outputText.text = Mathf.Max(x1, y1).ToString();
		}
		if (y2 > y1) {
			a = x1;
			x1 = x2;
			y1 = y2;
			x2 = a + 0.618f * (b - a);
			y2 = Mathf.Cos(x2);
		}
		else {
			b = x2;
			x2 = x1;
			y2 = y1;
			x1 = a + 0.382f * (b - a);
			y1 = Mathf.Cos(x1);
		}
	}

	void InitializePoints() {
		aPoint = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		bPoint = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		x1Point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		x2Point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		y1Point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);
		y2Point = Instantiate(pointPrefab, Vector3.zero, Quaternion.identity);

		aPoint.GetComponent<Point>().color = Color.green;
		bPoint.GetComponent<Point>().color = Color.green;
		x1Point.GetComponent<Point>().color = Color.blue;
		y1Point.GetComponent<Point>().color = Color.blue;
		x2Point.GetComponent<Point>().color = Color.red;
		y2Point.GetComponent<Point>().color = Color.red;
	}

}
